home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / games / nhak_src.zip / MON.C < prev    next >
C/C++ Source or Header  |  1993-03-16  |  38KB  |  1,416 lines

  1. /*    SCCS Id: @(#)mon.c    3.0    89/11/22
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5. /* Aztec C on amiga doesn't recognize defined() at this point!
  6.    Neither does the Mac Lightspeed C v.3  compiler. If you're using
  7.    precompiled headers, you don't want this either */
  8. #ifndef AZTEC_C
  9. #ifndef THINK_C
  10. #if defined(MICROPORT_BUG) || (!defined(LINT) && !defined(__STDC__))
  11. #define MKROOM_H
  12. #endif /* Avoid the microport bug */
  13. #endif
  14. #endif
  15.  
  16. #include "hack.h"
  17. #include "mfndpos.h"
  18.  
  19. #ifdef WORM
  20. #  include "wseg.h"
  21. #endif
  22.  
  23. #ifdef HARD
  24. STATIC_DCL boolean FDECL(restrap,(struct monst *));
  25. #endif
  26. #ifdef INFERNO
  27. #  include <ctype.h>
  28. #endif
  29.  
  30. STATIC_DCL void NDECL(dmonsfree);
  31.  
  32. #ifdef OVL1
  33. long lastwarntime;
  34. int lastwarnlev;
  35. const char *warnings[] = {
  36.     "white", "pink", "red", "ruby", "purple", "black" };
  37.  
  38. #endif /* OVL1 */
  39.  
  40. #ifdef OVLB
  41. static struct obj *FDECL(make_corpse,(struct monst *));
  42. static void FDECL(m_detach,(struct monst *));
  43.  
  44. struct monst *fdmon;  /* chain of dead monsters, need not be saved */
  45.               /* otherwise used only in priest.c */
  46.  
  47. /* Creates a monster corpse, a "special" corpse, or nothing if it doesn't
  48.  * leave corpses.  Monsters which leave "special" corpses should have
  49.  * G_NOCORPSE set in order to prevent wishing for one, finding tins of one,
  50.  * etc....
  51.  */
  52. static struct obj *
  53. make_corpse(mtmp)
  54. register struct monst *mtmp;
  55. {
  56.     register struct permonst *mdat = mtmp->data;
  57. #ifdef GOLEMS
  58.     int pieces;
  59. #endif
  60.     struct obj *obj = 0;
  61.     int x = mtmp->mx, y = mtmp->my;
  62.  
  63.     switch(monsndx(mdat)) {
  64.         case PM_WHITE_UNICORN:
  65.         case PM_GRAY_UNICORN:
  66.         case PM_BLACK_UNICORN:
  67.         (void) mksobj_at(UNICORN_HORN, x, y);
  68.         goto default_1;
  69. #ifdef WORM
  70.         case PM_LONG_WORM:
  71.         (void) mksobj_at(WORM_TOOTH, x, y);
  72.         goto default_1;
  73. #endif
  74.         case PM_KOBOLD_MUMMY:
  75.         obj = mksobj_at(MUMMY_WRAPPING, x, y); /* and fall through */
  76.         case PM_KOBOLD_ZOMBIE:
  77.         obj = mksobj_at(CORPSE, x, y);
  78.         obj->corpsenm = PM_KOBOLD;
  79.         obj->age -= 50;            /* this is an *OLD* corpse */
  80.         break;
  81.         case PM_GNOME_MUMMY:
  82.         obj = mksobj_at(MUMMY_WRAPPING, x, y); /* and fall through */
  83.         case PM_GNOME_ZOMBIE:
  84.         obj = mksobj_at(CORPSE, x, y);
  85.         obj->corpsenm = PM_GNOME;
  86.         obj->age -= 50;            /* this is an *OLD* corpse */
  87.         break;
  88.         case PM_ORC_MUMMY:
  89.         obj = mksobj_at(MUMMY_WRAPPING, x, y); /* and fall through */
  90.         case PM_ORC_ZOMBIE:
  91.         obj = mksobj_at(CORPSE, x, y);
  92.         obj->corpsenm = PM_ORC;
  93.         obj->age -= 50;            /* this is an *OLD* corpse */
  94.         break;
  95.         case PM_ELF_MUMMY:
  96.         obj = mksobj_at(MUMMY_WRAPPING, x, y); /* and fall through */
  97.         case PM_ELF_ZOMBIE:
  98.         obj = mksobj_at(CORPSE, x, y);
  99.         obj->corpsenm = PM_ELF;
  100.         obj->age -= 50;            /* this is an *OLD* corpse */
  101.         break;
  102.         case PM_HUMAN_MUMMY:
  103.         obj = mksobj_at(MUMMY_WRAPPING, x, y); /* and fall through */
  104.         case PM_HUMAN_ZOMBIE:
  105.         obj = mksobj_at(CORPSE, x, y);
  106.         obj->corpsenm = PM_HUMAN;
  107.         obj->age -= 50;            /* this is an *OLD* corpse */
  108.         break;
  109.         case PM_GIANT_MUMMY:
  110.         obj = mksobj_at(MUMMY_WRAPPING, x, y); /* and fall through */
  111.         case PM_GIANT_ZOMBIE:
  112.         obj = mksobj_at(CORPSE, x, y);
  113.         obj->corpsenm = PM_GIANT;
  114.         obj->age -= 50;            /* this is an *OLD* corpse */
  115.         break;
  116.         case PM_ETTIN_MUMMY:
  117.         obj = mksobj_at(MUMMY_WRAPPING, x, y); /* and fall through */
  118.         case PM_ETTIN_ZOMBIE:
  119.         obj = mksobj_at(CORPSE, x, y);
  120.         obj->corpsenm = PM_ETTIN;
  121.         obj->age -= 50;            /* this is an *OLD* corpse */
  122.         break;
  123. #ifdef GOLEMS
  124.         case PM_IRON_GOLEM:
  125.         pieces = d(2,6);
  126.         while (pieces--)
  127.             obj = mksobj_at(IRON_CHAIN, x, y);
  128.         break;
  129.         case PM_CLAY_GOLEM:
  130.         obj = mksobj_at(ROCK, x, y);
  131.         obj->quan = rn2(20) + 100;
  132.         obj->owt = weight(obj);
  133.         break;
  134.         case PM_STONE_GOLEM:
  135.         obj = mkcorpstat(STATUE, mdat, x, y);
  136.         break;
  137.         case PM_WOOD_GOLEM:
  138.         pieces = d(2,4);
  139.         while(pieces--)
  140.             obj = mksobj_at(QUARTERSTAFF, x, y);
  141.         break;
  142.         case PM_LEATHER_GOLEM:
  143.         pieces = d(2,4);
  144.         while(pieces--)
  145.             obj = mksobj_at(LEATHER_ARMOR, x, y);
  146.         break;
  147. #endif
  148.         default_1:
  149.         default:
  150.         if (mdat->geno & G_NOCORPSE)
  151.             return (struct obj *)0;
  152.         else obj = mkcorpstat(CORPSE, mdat, x, y);
  153.         break;
  154.     }
  155.     /* All special cases should precede the G_NOCORPSE check */
  156.  
  157.     /* Note: oname() cannot be used generically for non-inventory objects
  158.      * unless you fix the link from the previous object in the chains.
  159.      * (Here we know it's the first one, so there was no link.)
  160.      */
  161.     if (mtmp->mnamelth) {
  162.         obj = oname(obj, NAME(mtmp), 0);
  163.         fobj = obj;
  164.         level.objects[x][y] = obj;
  165.     }
  166.     stackobj(fobj);
  167.     newsym(x, y);
  168.     return obj;
  169. }
  170.  
  171. #endif /* OVLB */
  172. #ifdef OVL2
  173.  
  174. STATIC_OVL void
  175. dmonsfree(){
  176. register struct monst *mtmp;
  177.     while(mtmp = fdmon){
  178.         fdmon = mtmp->nmon;
  179.         free((genericptr_t) mtmp);
  180.     }
  181. }
  182.  
  183. #endif /* OVL2 */
  184. #ifdef OVL1
  185.  
  186. void
  187. movemon()
  188. {
  189.     register struct monst *mtmp;
  190.  
  191.     warnlevel = 0;
  192.  
  193.     while(1) {
  194.         /*  Find a monster that we have not treated yet.
  195.          *  Note that mtmp or mtmp->nmon might get killed
  196.          *  while mtmp moves, so we cannot just walk down the
  197.          *  chain (even new monsters might get created!)
  198.          */
  199.         /* Do tame monsters first.  Necessary so that when the tame
  200.          * monster attacks something, the something gets a chance to
  201.          * attack the tame monster back (which it's permitted to do
  202.          * only if it hasn't made its move yet).
  203.          */
  204.         for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
  205.             if(mtmp->mlstmv < moves && mtmp->mtame) goto next_mon;
  206.         for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
  207.             if(mtmp->mlstmv < moves && !mtmp->mtame) goto next_mon;
  208.         /* treated all monsters */
  209.         break;
  210.  
  211.     next_mon:
  212.         mtmp->mlstmv = moves;
  213.  
  214.         /* most monsters drown in pools */
  215.         { boolean inpool,iseel,isgremlin;
  216. #ifdef FOUNTAINS
  217.           boolean infountain;
  218. #endif
  219.  
  220.           inpool = is_pool(mtmp->mx,mtmp->my);
  221.           iseel = mtmp->data->mlet == S_EEL;
  222.           isgremlin = mtmp->data->mlet == S_GREMLIN;
  223. #ifdef FOUNTAINS
  224.           infountain = IS_FOUNTAIN(levl[mtmp->mx][mtmp->my].typ);
  225. #endif
  226.         /* Gremlin multiplying won't go on forever since the hit points
  227.          * keep going down, and when it gets to 1 hit point the clone
  228.          * function will fail.
  229.          */
  230.           if((inpool
  231. #ifdef FOUNTAINS
  232.                  || infountain
  233. #endif
  234.                       ) && isgremlin && rn2(3)) {
  235.             struct monst *mtmp2 = clone_mon(mtmp);
  236.  
  237.             if (mtmp2) {
  238.                 mtmp2->mhpmax = (mtmp->mhpmax /= 2);
  239.                 if(cansee(mtmp->mx,mtmp->my))
  240.                 pline("%s multiplies.", Monnam(mtmp));
  241.             }
  242. #ifdef FOUNTAINS
  243.             if (infountain) dryup();
  244. #endif
  245.           } else
  246.           if(inpool && !is_flyer(mtmp->data) && !is_swimmer(mtmp->data)) {
  247.             if(cansee(mtmp->mx,mtmp->my))
  248.                 pline("%s drowns.", Monnam(mtmp));
  249.             mondead(mtmp);
  250.             continue;
  251.           } else
  252.         /* but eels have a difficult time outside */
  253.           if(iseel && !inpool) {
  254.             if(mtmp->mhp > 1) mtmp->mhp--;
  255.             mtmp->mflee = 1;
  256.             mtmp->mfleetim += 2;
  257.           }
  258.         }
  259.         if(mtmp->mblinded && !--mtmp->mblinded)
  260.             mtmp->mcansee = 1;
  261.         if(mtmp->mfrozen && !--mtmp->mfrozen)
  262.             mtmp->mcanmove = 1;
  263.         if(mtmp->mfleetim && !--mtmp->mfleetim)
  264.             mtmp->mflee = 0;
  265. #ifdef HARD
  266.         /* unwatched mimics and piercers may hide again  [MRS] */
  267.         if(is_hider(mtmp->data) && restrap(mtmp))   continue;
  268. #endif
  269.         if(mtmp->mimic) continue;
  270.         if(mtmp->mspeed != MSLOW || !(moves%2)){
  271.         /* continue if the monster died fighting */
  272.           if (Conflict && !mtmp->iswiz && mtmp->mcansee) {
  273. /* Note: A couple of notes on conflict here.
  274.      1. Conflict does not take effect in the first round.  Therefore, 
  275.         A monster in a stepping into the area will get to swing at you.
  276.      2. Conflict still works when you are invisible.  (?)
  277.      3. Certain areas (namely castle) you can be in 3 "rooms" at once!
  278.         Polyself into Xorn wearing ring of conflict and it can be done.
  279.         This code only allows for two.  This needs to be changed if more
  280.         areas (with diggable walls and > 2 rooms) are put into the game.
  281. */
  282.             xchar clx = 0, chx = 0, cly = 0, chy = 0,
  283.               clx2 = 0, chx2 = 0, cly2 = 0, chy2 = 0;
  284.             /* seelx etc. are not set if blind or blindfolded! */
  285.             getcorners(&clx, &chx, &cly, &chy,
  286.                    &clx2, &chx2, &cly2, &chy2);
  287.             if ((dist(mtmp->mx,mtmp->my) < 3) || 
  288.             /* if the monster is next to you OR */
  289.              (levl[u.ux][u.uy].lit &&
  290.              levl[mtmp->mx][mtmp->my].lit &&
  291.             /* both you and it are lit AND */
  292.              ((clx <= mtmp->mx && mtmp->mx <= chx &&
  293.                cly <= mtmp->my && mtmp->my <= chy) ||
  294.               (clx2 <= mtmp->mx && mtmp->mx <= chx2 &&
  295.                cly2 <= mtmp->my && mtmp->my <= chy2))))
  296.             /* you *could* see it (ie it can see you) */
  297.               if (fightm(mtmp) != 3)
  298.               /* have it fight if it choses to */
  299.             continue;
  300.           }
  301.           if(dochugw(mtmp))
  302.           /* otherwise just move the monster */
  303.             continue;
  304.         }
  305.         if(mtmp->mspeed == MFAST && dochugw(mtmp))
  306.             continue;
  307.     }
  308. #ifdef NAMED_ITEMS
  309.     if (warnlevel == 100) {
  310.         Your("%s %s!", aobjnam(uwep, "glow"),
  311.             Hallucination ? hcolor() : light_blue);
  312.         warnlevel = 0;
  313.     }
  314. #endif
  315.     warnlevel -= u.ulevel;
  316.     if(warnlevel >= SIZE(warnings))
  317.         warnlevel = SIZE(warnings)-1;
  318.     if(!Blind && warnlevel >= 0)
  319.     if(warnlevel > lastwarnlev || moves > lastwarntime + 5){
  320.         register const char *rr;
  321.     
  322.         switch((int) (Warning & (LEFT_RING | RIGHT_RING))){
  323.         case LEFT_RING:
  324.         rr = "Your left ring glows";
  325.         break;
  326.         case RIGHT_RING:
  327.         rr = "Your right ring glows";
  328.         break;
  329.         case LEFT_RING | RIGHT_RING:
  330.         rr = "Both your rings glow";
  331.         break;
  332.         default:
  333.         { char buf[33];
  334.         Sprintf(buf, "Your %s glow", makeplural(body_part(FINGERTIP)));
  335.         rr = buf;
  336.         }
  337.         break;
  338.         }
  339.         pline("%s %s!", rr, Hallucination ? hcolor() : warnings[warnlevel]);
  340.         lastwarntime = moves;
  341.         lastwarnlev = warnlevel;
  342.     }
  343.  
  344.     dmonsfree();    /* remove all dead monsters */
  345. }
  346.  
  347. #endif /* OVL1 */
  348. #ifdef OVLB
  349.  
  350. void
  351. meatgold(mtmp)
  352.     register struct monst *mtmp;
  353. {
  354.     register struct gold *gold;
  355.     register struct obj *otmp;
  356.  
  357.     /* Eats gold if it is there */
  358.     if(gold = g_at(mtmp->mx, mtmp->my)){
  359.         if (cansee(mtmp->mx, mtmp->my) && flags.verbose)
  360.             pline("%s eats some gold!", Monnam(mtmp));
  361.         mtmp->meating = (int)((gold->amount + 500L)/1000L);
  362.         freegold(gold);
  363.         /* Left behind a pile? */
  364.         if(rnd(25) < 3) (void) mksobj_at(ROCK, mtmp->mx, mtmp->my);
  365.         newsym(mtmp->mx, mtmp->my);
  366.     }
  367.     /* Eats topmost metal object if it is there */
  368.     for (otmp = level.objects[mtmp->mx][mtmp->my];
  369.                             otmp; otmp = otmp->nexthere)
  370.         if (objects[otmp->otyp].oc_material > WOOD &&
  371.         objects[otmp->otyp].oc_material < MINERAL) {
  372.             if (cansee(mtmp->mx,mtmp->my) && flags.verbose)
  373.             pline("%s eats %s!", Monnam(mtmp),
  374.                 distant_name(otmp,doname));
  375.             else if (flags.soundok && flags.verbose)
  376.             You("hear a crunching sound.");
  377.             mtmp->meating = otmp->owt/2 - 1;
  378.             /* Heal up to the object's weight in hp */
  379.             if (mtmp->mhp < mtmp->mhpmax) {
  380.             mtmp->mhp += objects[otmp->otyp].oc_weight;
  381.             if (mtmp->mhp > mtmp->mhpmax) mtmp->mhp = mtmp->mhpmax;
  382.             }
  383.             if(otmp == uball) {
  384.             unpunish();
  385.             freeobj(otmp);
  386.             } else if(otmp == uchain)
  387.             unpunish();    /* frees uchain */
  388.             else
  389.             freeobj(otmp);
  390.             /* Left behind a pile? */
  391.             if(rnd(25) < 3) (void) mksobj_at(ROCK, mtmp->mx, mtmp->my);
  392.             newsym(mtmp->mx, mtmp->my);
  393.             break;
  394.         }
  395. }
  396.  
  397. void
  398. meatobj(mtmp)        /* for gelatinous cubes */
  399.     register struct monst *mtmp;
  400. {
  401.     register struct obj *otmp, *otmp2;
  402.  
  403.     /* Eats organic, glass, or wood objects if there */
  404.     /* Engulfs others, except huge rocks and metal attached to player */
  405.     for (otmp = level.objects[mtmp->mx][mtmp->my]; otmp; otmp = otmp2) {
  406.         otmp2 = otmp->nexthere;
  407.         if(objects[otmp->otyp].oc_material <= WOOD) {
  408.         if (cansee(mtmp->mx,mtmp->my) && flags.verbose)
  409.             pline("%s eats %s!", Monnam(mtmp),
  410.                 distant_name(otmp, doname));
  411.         else if (flags.soundok && flags.verbose)
  412.             You("hear a slurping sound.");
  413.         /* Heal up to the object's weight in hp */
  414.         if (mtmp->mhp < mtmp->mhpmax) {
  415.             mtmp->mhp += objects[otmp->otyp].oc_weight;
  416.             if (mtmp->mhp > mtmp->mhpmax) mtmp->mhp = mtmp->mhpmax;
  417.         }
  418.         delobj(otmp);        /* munch */
  419.         } else if (otmp->olet != ROCK_SYM &&
  420.                     otmp != uball && otmp != uchain) {
  421.         if (cansee(mtmp->mx, mtmp->my) && flags.verbose)
  422.             pline("%s engulfs %s.", Monnam(mtmp),
  423.                 distant_name(otmp,doname));
  424.         freeobj(otmp);
  425.         mpickobj(mtmp, otmp);    /* slurp */
  426.         }
  427.         /* Engulf & devour is instant, so don't set meating */
  428.         newsym(mtmp->mx, mtmp->my);
  429.     }
  430. }
  431.  
  432. void
  433. mpickgold(mtmp)
  434.     register struct monst *mtmp;
  435. {
  436.     register struct gold *gold;
  437.  
  438.     if(gold = g_at(mtmp->mx, mtmp->my)){
  439.         mtmp->mgold += gold->amount;
  440.         if (cansee(mtmp->mx, mtmp->my) && flags.verbose)
  441.             pline("%s picks up some gold.", Monnam(mtmp));
  442.         freegold(gold);
  443.         if(levl[mtmp->mx][mtmp->my].scrsym == GOLD_SYM)
  444.             newsym(mtmp->mx, mtmp->my);
  445.     }
  446. }
  447.  
  448. /* Now includes giants which pick up enormous rocks.  KAA */
  449. void
  450. mpickgems(mtmp)
  451.     register struct monst *mtmp;
  452. {
  453.     register struct obj *otmp;
  454.  
  455.     for(otmp = level.objects[mtmp->mx][mtmp->my]; otmp; otmp=otmp->nexthere)
  456.         if(throws_rocks(mtmp->data) ? otmp->otyp == BOULDER :
  457.             (otmp->olet == GEM_SYM && otmp->otyp < LAST_GEM+6))
  458.         if(mtmp->data->mlet != S_UNICORN
  459.             || objects[otmp->otyp].g_val != 0){
  460.             if (cansee(mtmp->mx,mtmp->my) && flags.verbose)
  461.             pline("%s picks up %s.", Monnam(mtmp),
  462.                 distant_name(otmp, doname));
  463.             freeobj(otmp);
  464.             mpickobj(mtmp, otmp);
  465.             newsym(mtmp->mx, mtmp->my);
  466.             return;    /* pick only one object */
  467.         }
  468. }
  469.  
  470. #endif /* OVLB */
  471. #ifdef OVL0
  472.  
  473. int
  474. curr_mon_load(mtmp)
  475. register struct monst *mtmp;
  476. {
  477.     register int curload = 0;
  478.     register struct obj *obj;
  479.  
  480.     for(obj = mtmp->minvent; obj; obj = obj->nobj) {
  481.         if(obj->otyp != BOULDER || !throws_rocks(mtmp->data))
  482.             curload += weight(obj);
  483.     }
  484.  
  485.     return curload;
  486. }
  487.  
  488. int
  489. max_mon_load(mtmp)
  490. register struct monst *mtmp;
  491. {
  492.     register int maxload;
  493.  
  494.     /* Base monster carrying capacity is equal to human maximum
  495.      * carrying capacity, or half human maximum if not strong.
  496.      * (for a polymorphed player, the value used would be the
  497.      * non-polymorphed carrying capacity instead of max/half max).
  498.      * This is then modified by the ratio between the monster weights
  499.      * and human weights (weight of a human=45).  Limits for corpseless
  500.      * monsters are arbitrary.
  501.      */
  502.     if (!mtmp->data->cwt)
  503.         maxload = MAX_CARR_CAP * (mtmp->data->mlevel * 6) / 45;
  504.     else if (!strongmonst(mtmp->data)
  505.         || (strongmonst(mtmp->data) && (mtmp->data->cwt > 45)))
  506.         maxload = MAX_CARR_CAP * mtmp->data->cwt / 45;
  507.     else    maxload = MAX_CARR_CAP;    /* strong monsters w/ cwt <= 45 */
  508.  
  509.     if (!strongmonst(mtmp->data)) maxload /= 2;
  510.  
  511.     return maxload;
  512. }
  513.  
  514. /* for restricting monsters' object-pickup */
  515. boolean
  516. can_carry(mtmp,otmp)
  517. struct monst *mtmp;
  518. struct obj *otmp;
  519. {
  520.     register int newload = weight(otmp);
  521.  
  522.     if (otmp->otyp == CORPSE && otmp->corpsenm == PM_COCKATRICE
  523.                         && !resists_ston(mtmp->data))
  524.         return(FALSE);
  525.     if (mtmp->isshk) return(TRUE); /* no limit */
  526.     if (mtmp->mpeaceful && !mtmp->mtame) return(FALSE);
  527.     /* otherwise players might find themselves obligated to violate
  528.      * their alignment if the monster takes something they need
  529.      */
  530.     
  531.     /* special--boulder throwers carry unlimited amounts of boulders */
  532.     if (throws_rocks(mtmp->data) && otmp->otyp == BOULDER)
  533.         return(TRUE);
  534.     
  535.     /* nymphs deal in stolen merchandise, but not boulders or statues */
  536.     if (mtmp->data->mlet == S_NYMPH)
  537.         return !(otmp->olet == ROCK_SYM);
  538.  
  539.     if(curr_mon_load(mtmp) + newload > max_mon_load(mtmp)) return(FALSE);
  540.  
  541.     return(TRUE);
  542. }
  543.  
  544. #endif /* OVL0 */
  545. #ifdef OVL2
  546.  
  547. void
  548. mpickstuff(mtmp, str)
  549.     register struct monst *mtmp;
  550.     register const char *str;
  551. {
  552.     register struct obj *otmp;
  553.  
  554. /*    prevent shopkeepers from leaving the door of their shop */
  555.     if(mtmp->isshk && inhishop(mtmp)) return;
  556.  
  557.     for(otmp = level.objects[mtmp->mx][mtmp->my]; otmp; otmp=otmp->nexthere)
  558.         if(index(str, otmp->olet)) {
  559.         if(!can_carry(mtmp,otmp)) return;
  560.         if (cansee(mtmp->mx,mtmp->my) && flags.verbose)
  561.             pline("%s picks up %s.", Monnam(mtmp), doname(otmp));
  562.         freeobj(otmp);
  563.         mpickobj(mtmp, otmp);
  564.         if(index(str, (char) levl[mtmp->mx][mtmp->my].scrsym))
  565.             newsym(mtmp->mx, mtmp->my);
  566.         return;            /* pick only one object */
  567.         }
  568. }
  569.  
  570. #endif /* OVL2 */
  571. #ifdef OVL0
  572.  
  573. /* return number of acceptable neighbour positions */
  574. int
  575. mfndpos(mon, poss, info, flag)
  576.     register struct monst *mon;
  577.     coord *poss;    /* coord poss[9] */
  578.     long *info;    /* long info[9] */
  579.     long flag;
  580. {
  581.     register int x,y,nx,ny,cnt = 0;
  582.     register uchar ntyp;
  583.     uchar nowtyp;
  584.     boolean wantpool,poolok,nodiag;
  585.  
  586.     x = mon->mx;
  587.     y = mon->my;
  588.     nowtyp = levl[x][y].typ;
  589.  
  590.     nodiag = (mon->data == &mons[PM_GRID_BUG]);
  591.     wantpool = mon->data->mlet == S_EEL;
  592.     poolok = is_flyer(mon->data) || (is_swimmer(mon->data) && !wantpool);
  593. nexttry:    /* eels prefer the water, but if there is no water nearby,
  594.            they will crawl over land */
  595.     if(mon->mconf) {
  596.         flag |= ALLOW_ALL;
  597.         flag &= ~NOTONL;
  598.     }
  599.     if(!mon->mcansee)
  600.         flag |= ALLOW_SSM;
  601.     for(nx = x-1; nx <= x+1; nx++) for(ny = y-1; ny <= y+1; ny++) {
  602.         if((nx == x && ny == y) || !isok(nx,ny)) continue;
  603.         if(nx != x && ny != y && nodiag) continue;
  604.         if(IS_ROCK(ntyp = levl[nx][ny].typ) && !(flag & ALLOW_WALL) &&
  605.         !((flag & ALLOW_DIG) && may_dig(nx,ny))) continue;
  606.         if(IS_DOOR(ntyp) && !amorphous(mon->data) &&
  607.            ((levl[nx][ny].doormask & D_CLOSED && !(flag & OPENDOOR)) ||
  608.         (levl[nx][ny].doormask & D_LOCKED && !(flag & UNLOCKDOOR))
  609.            ) && !(flag & (ALLOW_WALL|ALLOW_DIG|BUSTDOOR))) continue;
  610.         if(nx != x && ny != y &&
  611. #ifdef REINCARNATION
  612.            ((IS_DOOR(nowtyp) &&
  613.              ((levl[x][y].doormask & ~D_BROKEN) || dlevel == rogue_level)) ||
  614.         (IS_DOOR(ntyp) &&
  615.          ((levl[nx][ny].doormask & ~D_BROKEN) || dlevel == rogue_level))
  616. #else
  617.            ((IS_DOOR(nowtyp) && (levl[x][y].doormask & ~D_BROKEN)) ||
  618.         (IS_DOOR(ntyp) && (levl[nx][ny].doormask & ~D_BROKEN))
  619. #endif
  620.            ))
  621.         continue;
  622.         if(is_pool(nx,ny) == wantpool || poolok) {
  623.         /* Displacement also displaces the Elbereth/scare monster,
  624.          * as long as you are visible.
  625.          */
  626.         int dispx = (Displaced && (!Invis || perceives(mon->data)) &&
  627.             (mon->mux==nx)) ? u.ux : nx;
  628.         int dispy = (Displaced && (!Invis || perceives(mon->data)) &&
  629.             (mon->muy==ny)) ? u.uy : ny;
  630.  
  631.         info[cnt] = 0;
  632.         if(sobj_at(SCR_SCARE_MONSTER, dispx, dispy)
  633. #ifdef ELBERETH
  634.            || sengr_at("Elbereth", dispx, dispy)
  635. #endif
  636.           ) {
  637.             if(!(flag & ALLOW_SSM)) continue;
  638.             info[cnt] |= ALLOW_SSM;
  639.         }
  640.         if((nx == u.ux && ny == u.uy) ||
  641.            (nx == mon->mux && ny == mon->muy)) {
  642.             if(!(flag & ALLOW_U)) continue;
  643.             info[cnt] |= ALLOW_U;
  644.         } else {
  645.             if(MON_AT(nx, ny)) {
  646.                 if(!(flag & ALLOW_M)) continue;
  647.                 info[cnt] |= ALLOW_M;
  648.                 if((m_at(nx,ny))->mtame) {
  649.                     if(!(flag & ALLOW_TM)) continue;
  650.                     info[cnt] |= ALLOW_TM;
  651.                 }
  652.             }
  653. #if defined(ALTARS) && defined(THEOLOGY)
  654.             /* Note: ALLOW_SANCT only prevents movement, not */
  655.             /* attack, into a temple. */
  656.             if(!in_temple(x, y) && in_temple(nx, ny) &&
  657.                     u_in_sanctuary(in_temple(nx, ny))) {
  658.                 if(!(flag & ALLOW_SANCT)) continue;
  659.                 info[cnt] |= ALLOW_SANCT;
  660.             }
  661. #endif
  662.         }
  663.         if(sobj_at(CLOVE_OF_GARLIC, nx, ny)) {
  664.             if(flag & NOGARLIC) continue;
  665.             info[cnt] |= NOGARLIC;
  666.         }
  667.         if(sobj_at(BOULDER, nx, ny)) {
  668.             if(!(flag & ALLOW_ROCK)) continue;
  669.             info[cnt] |= ALLOW_ROCK;
  670.         }
  671.         if((!Invis || perceives(mon->data)) && online(nx,ny)){
  672.             if(flag & NOTONL) continue;
  673.             info[cnt] |= NOTONL;
  674.         }
  675.         /* we cannot avoid traps of an unknown kind */
  676.         { register struct trap *ttmp = t_at(nx, ny);
  677.           register long tt;
  678.             if(ttmp) {
  679. /*                tt = 1L << ttmp->ttyp;*/
  680. /* why don't we just have code look like what it's supposed to do? then it
  681. /* might start working for every case. try this instead: -sac */
  682.                 tt = (ttmp->ttyp < TRAPNUM && ttmp->ttyp);
  683.                 /* below if added by GAN 02/06/87 to avoid
  684.                  * traps out of range
  685.                  */
  686.                 if(!(tt & ALLOW_TRAPS))  {
  687. impossible("A monster looked at a very strange trap of type %d.", ttmp->ttyp);
  688.                     continue;
  689.                 }
  690.                 if(mon->mtrapseen & tt) {
  691.  
  692.                     if(!(flag & tt)) continue;
  693.                     info[cnt] |= tt;
  694.                 }
  695.             }
  696.         }
  697.         poss[cnt].x = nx;
  698.         poss[cnt].y = ny;
  699.         cnt++;
  700.         }
  701.     }
  702.     if(!cnt && wantpool && !is_pool(x,y)) {
  703.         wantpool = FALSE;
  704.         goto nexttry;
  705.     }
  706.     return(cnt);
  707. }
  708.  
  709. #endif /* OVL0 */
  710. #ifdef OVL1
  711.  
  712. int
  713. dist(x, y)
  714. register int x,y;
  715. {
  716.     register int dx = x - u.ux, dy = y - u.uy;
  717.     return dx*dx + dy*dy;
  718. }
  719.  
  720. boolean
  721. monnear(mon, x, y)
  722. register struct monst *mon;
  723. register int x,y;
  724. /* Is the square close enough for the monster to move or attack into? */
  725. {
  726.     register int distance = dist2(mon->mx, mon->my, x, y);
  727.     if (distance==2 && mon->data==&mons[PM_GRID_BUG]) return 0;
  728.     return (distance < 3);
  729. }
  730.  
  731. #endif /* OVL1 */
  732. #ifdef OVLB
  733.  
  734. static const char *poiseff[] = {
  735.  
  736.     " feel very weak", "r brain is on fire",
  737.     " can't think straight", "r muscles won't obey you",
  738.     " feel very sick", " break out in hives"
  739. };
  740.  
  741. void
  742. poisontell(typ)
  743.  
  744.     int    typ;
  745. {
  746.     pline("You%s.", poiseff[typ]);
  747. }
  748.  
  749. void
  750. poisoned(string, typ, pname, fatal)
  751. register const char *string, *pname;
  752. register int  typ, fatal;
  753. {
  754.     register int i, plural;
  755.     boolean thrown_weapon = !strncmp(string, "poison", 6);
  756.         /* admittedly a kludge... */
  757.  
  758.     if(strcmp(string, "blast") && !thrown_weapon) {
  759.         /* 'blast' has already given a 'poison gas' message */
  760.         /* so have "poison arrow", "poison dart", etc... */
  761.         plural = (string[strlen(string) - 1] == 's')? 1 : 0;
  762.         if(Blind)
  763.         pline("%s poisoned.", plural ? "They were" : "It was");
  764. #ifdef INFERNO
  765.         /* avoid "The" Orcus's sting was poisoned... */
  766.         else if(isupper(*string))
  767.         pline("%s %s poisoned!", string, plural ? "were" : "was");
  768. #endif
  769.         else
  770.         pline("The %s %s poisoned!", string, plural ? "were" : "was");
  771.     }
  772.  
  773.     if(Poison_resistance) {
  774.         if(!strcmp(string, "blast")) shieldeff(u.ux, u.uy);
  775.         pline("The poison doesn't seem to affect you.");
  776.         return;
  777.     }
  778.     i = rn2(fatal + 20*thrown_weapon);
  779.     if(i == 0 && typ != A_CHA) {
  780.         u.uhp = -1;
  781.         pline("The poison was deadly...");
  782.     } else if(i <= 5) {
  783.         You("%s!", poiseff[typ]);
  784.         adjattrib(typ, thrown_weapon ? -1 : -rn1(3,3), TRUE);
  785.     } else {
  786.         losehp(thrown_weapon ? rnd(6) : rn1(10,6), pname, KILLED_BY_AN);
  787.     }
  788.     if(u.uhp < 1) {
  789.         killer_format = KILLED_BY_AN;
  790.         killer = pname;
  791.         done(POISONING);
  792.     }
  793. }
  794.  
  795. static void
  796. m_detach(mtmp)
  797. register struct monst *mtmp;
  798. {
  799. #ifdef WALKIES
  800.     if(mtmp->mleashed) m_unleash(mtmp);
  801. #endif
  802.     relobj(mtmp,1);
  803.     unpmon(mtmp);
  804.     relmon(mtmp);
  805.     unstuck(mtmp);
  806. }
  807.  
  808. void
  809. mondead(mtmp)
  810. register struct monst *mtmp;
  811. {
  812.     m_detach(mtmp);    
  813. #ifdef KOPS
  814.     if(mtmp->data->mlet == S_KOP && allow_kops) {
  815.         /* Dead Kops may come back. */
  816.         switch(rnd(5)) {
  817.         case 1:         /* returns near the stairs */
  818.             (void) makemon(mtmp->data,xdnstair,ydnstair);
  819.             break;
  820.         case 2:         /* randomly */
  821.             (void) makemon(mtmp->data,0,0);
  822.             break;
  823.         default:
  824.             break;
  825.         }
  826.     }
  827. #endif
  828.     if(mtmp->isshk) shkdead(mtmp);
  829.     if(mtmp->isgd) {
  830.         if(!grddead(mtmp)) return;
  831.     }
  832. #ifdef WORM
  833.     if(mtmp->wormno) wormdead(mtmp);
  834. #endif
  835. #ifdef HARD
  836.     if(mtmp->iswiz) wizdead(mtmp);
  837. #endif
  838. #ifdef MEDUSA
  839.     if(mtmp->data == &mons[PM_MEDUSA]) u.ukilled_medusa = TRUE;
  840. #endif
  841.     monfree(mtmp);
  842. }
  843.  
  844. /* called when monster is moved to larger structure */
  845. void
  846. replmon(mtmp, mtmp2)
  847. register struct monst *mtmp, *mtmp2;
  848. {
  849.     relmon(mtmp);
  850.     monfree(mtmp);
  851.     place_monster(mtmp2, mtmp2->mx, mtmp2->my);
  852.     mtmp2->nmon = fmon;
  853.     fmon = mtmp2;
  854.     if(u.ustuck == mtmp) u.ustuck = mtmp2;
  855.     if(mtmp2->isshk) replshk(mtmp,mtmp2);
  856. #ifdef WORM
  857.     if(mtmp2->wormno) {
  858.         /* Each square the worm is on has a pointer; fix them all */
  859.         register struct wseg *wtmp;
  860.  
  861.         for(wtmp=wsegs[mtmp2->wormno]; wtmp; wtmp=wtmp->nseg)
  862.             place_worm_seg(mtmp2, wtmp->wx, wtmp->wy);
  863.     }
  864. #endif
  865. }
  866.  
  867. void
  868. relmon(mon)
  869. register struct monst *mon;
  870. {
  871.     register struct monst *mtmp;
  872.  
  873.     if (fmon == 0)  panic ("relmon: no fmon available.");
  874.  
  875.     remove_monster(mon->mx, mon->my);
  876.  
  877.     if(mon == fmon) fmon = fmon->nmon;
  878.     else {
  879.         for(mtmp = fmon; mtmp && mtmp->nmon != mon; mtmp = mtmp->nmon) ;
  880.         if(mtmp)    mtmp->nmon = mon->nmon;
  881.         else        panic("relmon: mon not in list.");
  882.     }
  883. }
  884.  
  885. /* we do not free monsters immediately, in order to have their name
  886.    available shortly after their demise */
  887. void
  888. monfree(mtmp) register struct monst *mtmp; {
  889.     mtmp->nmon = fdmon;
  890.     fdmon = mtmp;
  891.     remove_monster(mtmp->mx, mtmp->my);
  892. }
  893.  
  894. void
  895. unstuck(mtmp)
  896. register struct monst *mtmp;
  897. {
  898.     if(u.ustuck == mtmp) {
  899.         if(u.uswallow){
  900.             u.ux = mtmp->mx;
  901.             u.uy = mtmp->my;
  902.             u.uswallow = 0;
  903.             u.uswldtim = 0;
  904.             setsee();
  905.             docrt();
  906.         }
  907.         u.ustuck = 0;
  908.     }
  909. }
  910.  
  911. void
  912. killed(mtmp)
  913. register struct monst *mtmp;
  914. {
  915.     xkilled(mtmp, 1);
  916. }
  917.  
  918. void
  919. xkilled(mtmp, dest)
  920.     register struct monst *mtmp;
  921. /*
  922.  * Dest=1, normal; dest=0, don't print message; dest=2, don't drop corpse
  923.  * either; dest=3, message but no corpse
  924.  */
  925.     int    dest;
  926. {
  927.     register int tmp, nk, x, y;
  928.     register struct permonst *mdat = mtmp->data;
  929.     register struct obj *otmp;
  930.     boolean chance;
  931.  
  932.     if (dest & 1) {
  933.         if(!cansee(mtmp->mx,mtmp->my)) You("destroy it!");
  934.         else {
  935.         You("destroy %s!",
  936.             mtmp->mtame ? a2_monnam(mtmp, "poor") : mon_nam(mtmp));
  937.         }
  938.     }
  939.  
  940.     /* restore chameleon, lycanthropes to true form at death */
  941.     /* cannot do this in make_corpse() since genociding monsters after
  942.      * MAXMONNO were killed does the wrong type
  943.      */
  944.     if(mtmp->cham) mtmp->data = mdat = &mons[PM_CHAMELEON];
  945.     if(mdat == &mons[PM_JACKALWERE])
  946.         mtmp->data = mdat = &mons[PM_WEREJACKAL];
  947.     if(mdat == &mons[PM_WOLFWERE])
  948.         mtmp->data = mdat = &mons[PM_WEREWOLF];
  949.     if(mdat == &mons[PM_RATWERE])
  950.         mtmp->data = mdat = &mons[PM_WERERAT];
  951.  
  952.     /* if we have killed MAXMONNO monsters of a given type, and it
  953.      * can be done, genocide that monster.
  954.      */
  955.     tmp = monsndx(mdat);
  956.     u.nr_killed[tmp]++;
  957.     nk = u.nr_killed[tmp];
  958. #ifdef TOLKIEN
  959.     if(nk > (tmp==PM_NAZGUL ? 9 : MAXMONNO) &&
  960.                 !(mons[tmp].geno & (G_NOGEN | G_GENOD))) {
  961. #else
  962.     if(nk > MAXMONNO && !(mons[tmp].geno & (G_NOGEN | G_GENOD))) {
  963. #endif
  964. #ifdef DEBUG
  965.         pline("Automatically genocided %s.", makeplural(mons[tmp].mname));
  966. #endif
  967.         if (tmp != PM_WIZARD_OF_YENDOR)
  968.             mons[tmp].geno |= G_GENOD;
  969.     }
  970. #ifdef MAIL
  971.     /* If you kill the mail daemon, no more mail delivery.  -3. */
  972.     else if(tmp==PM_MAIL_DAEMON) mons[tmp].geno |= G_GENOD;
  973. #endif    
  974.  
  975.     /* punish bad behaviour */
  976.     if(is_human(mdat) && !always_hostile(mdat) &&
  977.        (monsndx(mdat) < PM_ARCHEOLOGIST || monsndx(mdat) > PM_WIZARD) &&
  978.        u.ualigntyp != U_CHAOTIC) {
  979.         HTelepat &= ~INTRINSIC;
  980.         change_luck(-2);
  981.     }
  982.     if((mtmp->mpeaceful && !rn2(2)) || mtmp->mtame)    change_luck(-1);
  983.     if ((mdat==&mons[PM_BLACK_UNICORN] && u.ualigntyp == U_CHAOTIC) ||
  984.         (mdat==&mons[PM_GRAY_UNICORN] && u.ualigntyp == U_NEUTRAL) ||
  985.         (mdat==&mons[PM_WHITE_UNICORN] && u.ualigntyp == U_LAWFUL))
  986.         change_luck(-5);
  987.  
  988.     /* give experience points */
  989.     tmp = experience(mtmp, nk);
  990.     more_experienced(tmp, 0);
  991.     newexplevel();        /* will decide if you go up */
  992.  
  993.     /* adjust alignment points */
  994.     if(mtmp->mtame)
  995.         adjalign(-15);    /* bad!! */
  996. #if defined(ALTARS) && defined(THEOLOGY)
  997.     else if (mtmp->ispriest && !p_coaligned(mtmp))
  998.         adjalign(2);
  999. #endif
  1000.     else if (mtmp->mpeaceful)
  1001.         adjalign(-5);
  1002.     /* malign was already adjusted for ualigntyp and randomization */
  1003.     adjalign(mtmp->malign);
  1004.  
  1005.     /* dispose of monster and make cadaver */
  1006.     if(stoned) {
  1007.         monstone(mtmp);
  1008.         return;
  1009.     }
  1010.  
  1011.     x = mtmp->mx;   y = mtmp->my;
  1012.  
  1013.     mondead(mtmp);
  1014.  
  1015.     if((dest & 2)
  1016. #ifdef REINCARNATION
  1017.          || dlevel == rogue_level
  1018. #endif
  1019.                     ) return;
  1020.  
  1021. #ifdef MAIL
  1022.     if(mdat == &mons[PM_MAIL_DAEMON]) {
  1023.         (void) mksobj_at(SCR_MAIL, x, y);
  1024.         stackobj(fobj);
  1025.         newsym(x,y);
  1026.     }
  1027. #endif
  1028.     if(!accessible(x, y)) {
  1029.         /* might be mimic in wall or dead eel*/
  1030.          newsym(x,y);
  1031.     } else if(x != u.ux || y != u.uy) {
  1032.         /* might be here after swallowed */
  1033.         if (!rn2(6) && !(mdat->geno & G_NOCORPSE)
  1034. #ifdef KOPS
  1035.                     && mdat->mlet != S_KOP
  1036. #endif
  1037.                             ) {
  1038.             int typ;
  1039.  
  1040.             otmp = mkobj_at(RANDOM_SYM, x, y, TRUE);
  1041.             /* Don't create large objects from small monsters */
  1042.             typ = otmp->otyp;
  1043.             if (mdat->msize < MZ_HUMAN && typ != FOOD_RATION
  1044. #ifdef WALKIES
  1045.                 && typ != LEASH
  1046. #endif
  1047.                 && typ != FIGURINE
  1048.                 && (otmp->owt > 3 ||
  1049.                 (typ >= SPEAR && typ <= LANCE) ||
  1050.                 (typ >= SCIMITAR && typ <= KATANA) ||
  1051.                 (typ == MORNING_STAR || typ == QUARTERSTAFF) ||
  1052.                 (typ >= BARDICHE && typ <= VOULGE) ||
  1053.                 (typ>=PLATE_MAIL && typ<=DRAGON_SCALE_MAIL) ||
  1054.                 (typ == LARGE_SHIELD))) {
  1055.                 delobj(otmp);
  1056.             } else newsym(x,y);
  1057.         }
  1058.         /* Whether or not it always makes a corpse is, in theory,
  1059.          * different from whether or not the corpse is "special";
  1060.          * if we want both, we have to specify it explicitly.
  1061.          */
  1062.         if (bigmonst(mdat) || mdat == &mons[PM_LIZARD]
  1063. #ifdef GOLEMS
  1064.                    || is_golem(mdat)
  1065. #endif
  1066.            ) chance = 1;
  1067.         else chance = !rn2((int)
  1068.             (2 + ((mdat->geno & G_FREQ)<2) + verysmall(mdat)));
  1069.         if (chance)
  1070.             (void) make_corpse(mtmp);
  1071.     }
  1072. }
  1073.  
  1074. void
  1075. rescham() {    /* force all chameleons to become normal */
  1076.  
  1077.     register struct monst *mtmp;
  1078.  
  1079.     for(mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
  1080.         if(mtmp->cham) {
  1081.             mtmp->cham = 0;
  1082.             (void) newcham(mtmp, &mons[PM_CHAMELEON]);
  1083.         }
  1084.         if(is_were(mtmp->data) && mtmp->data->mlet != S_HUMAN)
  1085.             (void) new_were(mtmp);
  1086.         if(mtmp->mimic && cansee(mtmp->mx, mtmp->my)) {
  1087.             seemimic(mtmp);
  1088.             /* we pretend that the mimic doesn't */
  1089.             /* know that it has been unmasked.   */
  1090.             mtmp->msleep = 1;
  1091.         }
  1092.     }
  1093. }
  1094.  
  1095. /* Let the chameleons change again -dgk */
  1096. void
  1097. restartcham() {
  1098.  
  1099.     register struct monst *mtmp;
  1100.  
  1101.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
  1102.         if (mtmp->data == &mons[PM_CHAMELEON])
  1103.             mtmp->cham = 1;
  1104.         if(mtmp->data->mlet == S_MIMIC && mtmp->msleep &&
  1105.                 cansee(mtmp->mx, mtmp->my)) {
  1106.             set_mimic_sym(mtmp);
  1107.             unpmon(mtmp);
  1108.             pmon(mtmp);
  1109.         }
  1110.     }
  1111. }
  1112.  
  1113. int
  1114. newcham(mtmp, mdat)    /* make a chameleon look like a new monster */
  1115.             /* returns 1 if the monster actually changed */
  1116.     register struct monst *mtmp;
  1117.     register struct permonst *mdat;
  1118. {
  1119.     register int mhp, hpn, hpd;
  1120.     int tryct;
  1121.        struct permonst *olddata = mtmp->data;
  1122.  
  1123.     /* mdat = 0 -> caller wants a random monster shape */
  1124.     tryct = 0;
  1125.     if(mdat == 0) {
  1126.         while (++tryct < 100) {
  1127.             static int NEARDATA num;
  1128.             mdat = &mons[num=rn2(NUMMONS)];
  1129.             if ((!is_human(mdat) || num == PM_NURSE)
  1130.                 && !type_is_pname(mdat)
  1131.                 && !is_were(mdat)
  1132. #ifdef MEDUSA
  1133.                 && num != PM_MEDUSA
  1134. #endif
  1135. #ifdef MAIL
  1136.                 && num != PM_MAIL_DAEMON
  1137. #endif
  1138.                 )
  1139.                 break;
  1140.         }
  1141.         if (tryct >= 100) return(0); /* Should never happen */
  1142.     }
  1143.       if(mdat == mtmp->data) return(0);    /* still the same monster */
  1144.  
  1145. #ifdef WORM
  1146.     if(mtmp->wormno) wormdead(mtmp);    /* throw tail away */
  1147. #endif
  1148.     hpn = mtmp->mhp;
  1149.      hpd = (mtmp->m_lev < 50) ? (mtmp->m_lev)*8 : mdat->mlevel;
  1150.      if(!hpd) hpd = 4;
  1151.  
  1152.     mtmp->m_lev = adj_lev(mdat);        /* new monster level */
  1153.  
  1154.      mhp = (mtmp->m_lev < 50) ? (mtmp->m_lev)*8 : mdat->mlevel;
  1155.      if(!mhp) mhp = 4;
  1156.  
  1157.     /* new hp: same fraction of max as before */
  1158. #ifndef LINT
  1159.      mtmp->mhp = (int)(((long)hpn*(long)mhp)/(long)hpd);
  1160. #endif
  1161.      if(mtmp->mhp < 0) mtmp->mhp = hpn;    /* overflow */
  1162. /* Unlikely but not impossible; a 1HD creature with 1HP that changes into a
  1163.    0HD creature will require this statement */
  1164.      if (!mtmp->mhp) mtmp->mhp = 1;
  1165.  
  1166. /* and the same for maximum hit points */
  1167.     hpn = mtmp->mhpmax;
  1168. #ifndef LINT
  1169.      mtmp->mhpmax = (int)(((long)hpn*(long)mhp)/(long)hpd);
  1170. #endif
  1171.      if(mtmp->mhpmax < 0) mtmp->mhpmax = hpn;    /* overflow */
  1172.      if (!mtmp->mhpmax) mtmp->mhpmax = 1;
  1173.  
  1174.      mtmp->data = mdat;
  1175.     mtmp->minvis = !!(mdat->mlet == S_STALKER);
  1176.     mtmp->mhide = !!hides_under(mdat);
  1177.     if (!mtmp->mhide) mtmp->mundetected = 0;
  1178.     if (u.ustuck == mtmp) {
  1179.         if(u.uswallow) {
  1180.             if(!attacktype(mdat,AT_ENGL)) {
  1181.                 /* Does mdat care? */
  1182.                 if (!noncorporeal(mdat) && !amorphous(mdat) && 
  1183.                     !is_whirly(mdat) && 
  1184.                     (mdat != &mons[PM_YELLOW_LIGHT])) {
  1185.                     You("break out of %s%s!", mon_nam(mtmp),
  1186.                         (is_animal(mdat)? 
  1187.                         "'s stomach" : ""));
  1188.                     mtmp->mhp = 1;  /* almost dead */
  1189.                 }
  1190.                 expels(mtmp, olddata, FALSE);
  1191.             }
  1192.         } else {
  1193.             if(!sticks(mdat)
  1194. #ifdef POLYSELF
  1195.                 && !sticks(uasmon)
  1196. #endif
  1197.                 )
  1198.                 unstuck(mtmp);
  1199.         }
  1200.     }
  1201.  
  1202. #ifdef WORM
  1203.     if(mdat == &mons[PM_LONG_WORM] && getwn(mtmp)) initworm(mtmp);
  1204. #endif
  1205.     unpmon(mtmp);    /* necessary for 'I' and to force pmon */
  1206.     pmon(mtmp);
  1207.     return(1);
  1208. }
  1209.  
  1210. void
  1211. mnexto(mtmp)    /* Make monster mtmp next to you (if possible) */
  1212.     struct monst *mtmp;
  1213. {
  1214.     coord mm;
  1215.     if(!enexto(&mm, u.ux, u.uy, mtmp->data)) return;
  1216.     remove_monster(mtmp->mx, mtmp->my);
  1217.     place_monster(mtmp, mm.x, mm.y);
  1218.     pmon(mtmp);
  1219.     set_apparxy(mtmp);
  1220. }
  1221.  
  1222. void
  1223. mnearto(mtmp,x,y,gz)    /* Make monster near (or at) location if possible */
  1224.     register struct monst *mtmp;
  1225.     xchar x, y;
  1226.     boolean gz;     
  1227. {
  1228.     coord mm;
  1229.     if(!gz || !goodpos(x,y,mtmp->data)) {
  1230.         if(!enexto(&mm, x, y, mtmp->data)) return;
  1231.         x = mm.x; y = mm.y;
  1232.     }
  1233.     if(x == mtmp->mx && y == mtmp->my) /* that was easy */
  1234.         return;
  1235.     remove_monster(mtmp->mx, mtmp->my);
  1236.     place_monster(mtmp, x, y);
  1237.     pmon(mtmp);
  1238.     set_apparxy(mtmp);
  1239. }
  1240.  
  1241. #endif /* OVLB */
  1242. #ifdef OVL2
  1243.  
  1244. void
  1245. setmangry(mtmp)
  1246.     register struct monst *mtmp;
  1247. {
  1248.     if(!mtmp->mpeaceful) return;
  1249.     if(mtmp->mtame) return;
  1250.     mtmp->mpeaceful = 0;
  1251. #if defined(ALTARS) && defined(THEOLOGY)
  1252.     if(mtmp->ispriest) {
  1253.         if(p_coaligned(mtmp)) adjalign(-5); /* very bad */
  1254.         else adjalign(2);
  1255.     } else
  1256. #endif
  1257.     adjalign(-1);        /* attacking peaceful monsters is bad */
  1258.     if(humanoid(mtmp->data) || mtmp->isshk || mtmp->isgd)
  1259.         pline("%s gets angry!", Monnam(mtmp));
  1260. #ifdef SOUNDS
  1261.     else if (flags.verbose && flags.soundok) growl(mtmp);
  1262. #endif
  1263. }
  1264.  
  1265. int
  1266. disturb(mtmp)        /* awaken monsters while in the same room.
  1267.              * return a 1 if they have been woken.
  1268.              */
  1269.     register struct monst *mtmp;
  1270. {
  1271.     /* wake up, or get out of here. */
  1272.     /* ettins are hard to surprise */
  1273.     /* Nymphs and Leprechauns do not easily wake up */
  1274.     if(cansee(mtmp->mx,mtmp->my) &&
  1275.         (!Stealth || (mtmp->data == &mons[PM_ETTIN] && rn2(10))) &&
  1276.         (!(mtmp->data->mlet == S_NYMPH
  1277.             || mtmp->data == &mons[PM_JABBERWOCK]
  1278.             || mtmp->data->mlet == S_LEPRECHAUN) || !rn2(50)) &&
  1279.         (Aggravate_monster ||
  1280.          (mtmp->data->mlet == S_DOG || mtmp->data->mlet == S_HUMAN) ||
  1281.         (!rn2(7) && !mtmp->mimic))) {
  1282.         mtmp->msleep = 0;
  1283.         return(1);
  1284.     }
  1285.     if(Hallucination) pmon(mtmp);
  1286.     return(0);
  1287. }
  1288.  
  1289. #ifdef HARD
  1290. STATIC_OVL boolean
  1291. restrap(mtmp)
  1292. /* unwatched hiders may hide again,
  1293.  * if so, a 1 is returned.
  1294.  */
  1295. register struct monst *mtmp;
  1296. {
  1297.     if(mtmp->cham || mtmp->mcan || mtmp->mimic ||
  1298.        cansee(mtmp->mx, mtmp->my) || rn2(3))
  1299.         return(FALSE);
  1300.  
  1301.     if(mtmp->data->mlet == S_MIMIC) {
  1302.         set_mimic_sym(mtmp);
  1303.         return(TRUE);
  1304.     } else
  1305.         if(levl[mtmp->mx][mtmp->my].typ == ROOM)  {
  1306.         (void) maketrap(mtmp->mx, mtmp->my, MONST_TRAP);
  1307.         /* override type selection */
  1308.         ftrap->pm = monsndx(mtmp->data);
  1309.         mondead(mtmp);
  1310.         return(TRUE);
  1311.         }
  1312.  
  1313.     return(FALSE);
  1314. }
  1315. #endif
  1316.  
  1317. #endif /* OVL2 */
  1318. #ifdef OVLB
  1319.  
  1320. /* drop (perhaps) a cadaver and remove monster */
  1321. void
  1322. mondied(mdef)
  1323. register struct monst *mdef;
  1324. {
  1325.     mondead(mdef);
  1326.     if(rn2(3)
  1327. #ifdef REINCARNATION
  1328.        && dlevel != rogue_level
  1329. #endif
  1330.                     )
  1331.         (void) make_corpse(mdef);
  1332. }
  1333.  
  1334. /* monster disappears, not dies */
  1335. void
  1336. mongone(mdef)
  1337. register struct monst *mdef;
  1338. {
  1339.     register struct obj *otmp, *otmp2;
  1340.  
  1341.     /* release monster's inventory */
  1342.     for (otmp = mdef->minvent; otmp; otmp = otmp2) {
  1343.         otmp2 = otmp->nobj;
  1344.         obfree(otmp, (struct obj *)0);
  1345.     }
  1346.     mdef->minvent = 0;
  1347.     mdef->mgold = 0;
  1348.     m_detach(mdef);
  1349.     monfree(mdef);
  1350. }
  1351.  
  1352. /* drop a statue or rock and remove monster */
  1353. void
  1354. monstone(mdef)
  1355. register struct monst *mdef;
  1356. {
  1357.     struct obj *otmp;
  1358.  
  1359.     if((int)mdef->data->msize > MZ_TINY ||
  1360.        !rn2(2 + ((mdef->data->geno & G_FREQ) > 2))) {
  1361.         otmp = mk_named_object(STATUE, mdef->data, mdef->mx, mdef->my,
  1362.             NAME(mdef), (int)mdef->mnamelth);
  1363.         otmp->spe = 0; /* no book inside */
  1364.     } else
  1365.         (void) mksobj_at(ROCK, mdef->mx, mdef->my);
  1366.  
  1367.     stackobj(fobj);
  1368.  
  1369.     if(cansee(mdef->mx, mdef->my)){
  1370.         unpmon(mdef);
  1371.         atl(mdef->mx,mdef->my,Hallucination ? rndobjsym() : fobj->olet);
  1372.     }
  1373.     mondead(mdef);
  1374. }
  1375.  
  1376. #ifdef GOLEMS
  1377. void
  1378. golemeffects(mon, damtype, dam)
  1379. register struct monst *mon;
  1380. int damtype, dam;
  1381. {
  1382.     int heal=0, slow=0;
  1383.  
  1384.     if (mon->data != &mons[PM_FLESH_GOLEM]
  1385.                     && mon->data != &mons[PM_IRON_GOLEM])
  1386.         return;
  1387.  
  1388.     if (mon->data == &mons[PM_FLESH_GOLEM]) {
  1389.         if (damtype == AD_ELEC) heal = dam / 6;
  1390.         else if (damtype == AD_FIRE || damtype == AD_COLD) slow = 1;
  1391.     } else {
  1392.         if (damtype == AD_ELEC) slow = 1;
  1393.         else if (damtype == AD_FIRE) heal = dam;
  1394.     }
  1395.     if (slow) {
  1396.         if (mon->mspeed != MSLOW) {
  1397.             if (mon->mspeed == MFAST) mon->mspeed = 0;
  1398.             else mon->mspeed = MSLOW;
  1399.             if (cansee(mon->mx, mon->my))
  1400.                 pline("%s seems to be moving slower.",
  1401.                     Monnam(mon));
  1402.         }
  1403.     }
  1404.     if (heal) {
  1405.         if (mon->mhp < mon->mhpmax) {
  1406.             mon->mhp += dam;
  1407.             if (mon->mhp > mon->mhpmax) mon->mhp = mon->mhpmax;
  1408.             if (cansee(mon->mx, mon->my))
  1409.                 pline("%s seems healthier.", Monnam(mon));
  1410.         }
  1411.     }
  1412. }
  1413. #endif /* GOLEMS */
  1414.  
  1415. #endif /* OVLB */
  1416.